<?php

namespace Parse;

class RegexParser extends Parser
{
    private $pattern;

    public function __construct($pattern, $callback = null)
    {
        if (!is_string($pattern)) {
            throw new ParseException("Pattern must be string");
        }
        $this->pattern = $pattern;
        parent::__construct($callback);
    }
    public function defaultCallback()
    {
        return func_get_arg(0);
    }

    public function matchProcedure($string)
    {
        if (preg_match($this->pattern, $string, $matches) === 1) {
            return array(
                "matches" => array($matches[0]),
                "rest" => substr($string, strlen($matches[0])));
        }
        throw new ParseException("RegexParser couldn't match expression");
    }
}
